package com.itic.appbase.applications.itext;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itic.appbase.applications.itext.utils.ItextHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
/**
 * itext实现word导出
 * <p>Company: itic</p>
 * @author: shijun
 * @date: 2015年6月17日 下午3:39:18
 * @version: V1.0
 */
@SuppressWarnings("unchecked")
public class ExportWordWithItext extends ExportWord{
    
    private static final Logger log = LoggerFactory.getLogger(ExportWordWithItext.class);
    
    //加载iTextAsian自带中文字体
    private static final BaseFont BASEFONT = ItextHelper.loadBaseFont();
    //文档头title信息字体大小
    private static final float TITLESIZE = 20;
    //表头thead字体大小
    private static final float THEADSIZE = 15;
    //表主体body字体大小
    private static final float TBODYSIZE = 12;
    //单元格合并的最小数量
    private static final int MINSPANNUM = 2;
    
    @Override
    protected void exportAutoWord(Map<String, Object> mapConfig,OutputStream out) throws IOException {
        if(null!=mapConfig && !mapConfig.isEmpty()){
            //标题
            String title = null==mapConfig.get("title")?null:(String)mapConfig.get("title");
            //表头数据
            List<WordCell> theadList =null==mapConfig.get("theadData")?null:(List<WordCell>) mapConfig.get("theadData");
            //主体数据
            List<WordCell> bodyList =null==mapConfig.get("tbodyData")?null:(List<WordCell>) mapConfig.get("tbodyData");
            //列宽
            int[] colWidths = null==mapConfig.get("colWidths")?null:(int[])mapConfig.get("colWidths");
            //加载数据
            loadDataToWord(title,theadList,bodyList,colWidths,out);
        }
    }
    
    /**
     * 加载word数据
     * @param title 标题
     * @param titleList 表头
     * @param bodyList 主体数据
     * @throws IOException 
     */
    private void loadDataToWord(String title, List<WordCell> theadList, List<WordCell> bodyList,int[] colWidths,OutputStream out) throws IOException{
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Document document = ItextHelper.createDocument(os );//创建文档对象
        ItextHelper.openDoucument(document);//打开文档对象
        //添加文档头信息
        if(StringHelper.isNotBlank(title)){
            Paragraph titleParagraph = ItextHelper.createParagraph(title, this.createTitleFont());
            //设置标题格式对齐方式
            titleParagraph.setAlignment(Element.ALIGN_CENTER);//居中
            ItextHelper.addDocumentElement(document, titleParagraph);//添加titleParagraph到文档对象
            //ItextHelper.contentWrap(document);//换行
        }
        //创建表格
        if(null!=colWidths && colWidths.length>0){
            Table table = ItextHelper.createTable(colWidths.length);
            if(null==table){
                log.error("table创建失败");
            }else{
                ItextHelper.setTableWidths(colWidths, table);//设置表格列宽
                table = loadTableData(table, theadList,true);//加载表头数据
                table = loadTableData(table, bodyList,false);//加载表格主体数据
                ItextHelper.addDocumentElement(document, table);//添加table到文档对象
            }
        }
        //输出word流
        ItextHelper.wordWriter(os, document, out);
    }
    /**
     * 加载表格数据
     * @param table 
     * @param dataList 
     * @param isHead  表头数据true;主体数据false
     * @return Table
     */
    private Table loadTableData(Table table,List<WordCell> dataList,boolean isHead){
        if(null!=dataList && !dataList.isEmpty()){
            Font dataFont = this.createTheadFont();//表头字体
            if(isHead){
                dataFont = this.createTheadFont();//表头字体
            }else{
                dataFont = this.createTbodyFont();//表格主体字体
            }
            for(WordCell cellData : dataList){
                   Chunk chunk = new Chunk(cellData.getCellValue(), dataFont);
                   Cell dataCell = ItextHelper.createCell(chunk);
                   if(null!=cellData.getRowSpan()&& cellData.getRowSpan()>=MINSPANNUM){//合并行
                       dataCell.setRowspan(cellData.getRowSpan());
                   }
                   if(null!=cellData.getColSpan() && cellData.getColSpan()>=MINSPANNUM){//合并列
                       dataCell.setColspan(cellData.getColSpan());
                   }
                   dataCell.setHeader(isHead);//设置为head
                   dataCell.setHorizontalAlignment(Cell.ALIGN_CENTER);//横向居中
                   dataCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);//竖向居中
                   ItextHelper.addTableCell(table, dataCell);
            }
        }
        return table;
    }
    
    /**
     * 文档头title信息字体
     * @return
     */
    private Font createTitleFont(){
        return ItextHelper.setFont(BASEFONT, TITLESIZE, Font.BOLD,Color.BLACK);
    }
    /**
     * 设置表头thead字体
     * @return
     */
    private Font createTheadFont(){
        return ItextHelper.setFont(BASEFONT, THEADSIZE, Font.BOLD);
    }
    /**
     * 设置表头tbody字体
     * @return
     */
    private Font createTbodyFont(){
        return ItextHelper.setFont(BASEFONT, TBODYSIZE, Font.NORMAL);
    }
}
